
#include <string>
#include <iostream>
#include <url/url_string.hh>
#include <vector>
#include <stdexcept>

void display_url(const url_string& url)
	{
	std::cout << "original:    " << url.get_original() << std::endl;
	if(url.get_referrer().size()!=0) {
		std::cout << "referrer:    " 
					 << url_string(url.get_referrer()).get_url() << std::endl;
		}
	std::cout << "relative:    " << url.get_relative_path() << std::endl
		  << "url:         " << url.get_url() << std::endl
		  << "object:      " << url.get_object() << std::endl
		  << "["<<url.get_scheme()<<"]"
		  << "//["<<url.get_username()<<"]"
		  << ":["<<url.get_passwd()<<"]"
		  << "@["<<url.get_net_path()<<"]"
		  << ":["<<url.get_port()<<"]"
		  << "["<<url.get_absolute_path()<<"]"
		  << ";["<<url.get_parameters()<<"]"
		  << "?["<<url.get_query()<<"]"
		  << "#["<<url.get_fragment()<<"]" << std::endl
		  << "-----" << std::endl << std::endl;
	}

int main(int argc, char *argv[]) 
	{
	try {
		std::string urlstr,refstr;
		
		refstr=urlstr="scheme://user:pass@host:999/path/file;param?query#fragment";
		url_string url1(urlstr);
		std::cout << urlstr << std::endl;
		display_url(url1);

		urlstr="/path2/file2";
		url_string url2(urlstr,url1);
		std::cout << urlstr << " " << refstr << std::endl;
		display_url(url2);
		
		urlstr="path3/file3";
		url2=url_string(urlstr,url1);
		std::cout << urlstr << " " << refstr << std::endl;
		display_url(url2);
		
		urlstr="file4";
		url2=url_string(urlstr, url1);
		std::cout << urlstr << " " << refstr << std::endl;
		display_url(url2);
		
		urlstr="//host2";
		url2=url_string(urlstr, url1);
		std::cout<<urlstr<<" "<<refstr<<std::endl;
		display_url(url2);
		
		urlstr="#fragment2";
		url2=url_string(urlstr, url1);
		std::cout << urlstr << " " << refstr << std::endl;
		display_url(url2);
		
		urlstr="?query2";
		url2=url_string(urlstr, url1);
		std::cout << urlstr << " " << refstr << std::endl;
		display_url(url2);
		
		urlstr=";param2";
		url2=url_string(urlstr, url1);
		std::cout << urlstr << " " << refstr << std::endl;
		display_url(url2);
		
		urlstr="http://scheme://host/path/file #chap1";
		url1=url_string(urlstr);
		std::cout << urlstr << std::endl;
		display_url(url1);
		
		urlstr="http://host.at.net/./path";
		url1=url_string(urlstr);
		std::cout << urlstr << std::endl;
		display_url(url1);

		// Correct: Should work properly
		urlstr="http://www.google.com/";
		url1=url_string(urlstr);
		url2=url_string("/some/relative/url", url1);
		url_string url3("one/final/relative/url", url2);
		display_url(url3);

		bool exception_caught = false;
		try {
			// Error: Should throw an exception (no referrer)
			urlstr="www.google.com";
			url1=url_string(urlstr);
			std::cout << urlstr << std::endl;
			display_url(url1);
			}
		catch ( std::logic_error& ex ) {
			exception_caught = true;
			}
		
		if ( ! exception_caught )
			throw std::logic_error( "An exception was expected but not caught" );
		}
	catch(std::exception& ex) {
		std::cerr << "exception: " << ex.what() << std::endl;
		}
	}
